/// Creator:      gaoruishan
/// CreatDate:    2025-08-12
/// Description:  输血相关
Class Nur.MNISV3.Service.Blood Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 获取条码信息--输血统一扫码调用
/// 涉及表：1,血袋记录 Nur.MNISV3.BloodBagRec 2,血袋记录明细 Nur.MNISV3.BloodBagSubRec
///       3,节点配置  CF.NUR.MNISV3.BloodNode 4,模板组件 CF.NUR.MNISV3.ModelForm  5,模板字段配置 CF.NUR.MNISV3.ModelItem
/// 初始化配置信息: d ##class(Nur.MNISV3.Init).blood()
/// w ##class(Nur.MNISV3.Service.Blood).GetBloodInfoV4("P0020","{""bloodPref"":"""",""bloodProductId"":"""",""bloodbagId"":""0010406020005"",""episodeId"":"""",""regNo"":""""}","2")
ClassMethod GetBloodInfoV4(barCode, barCodeJson, hospitalId = "")
{
    n (barCode,barCodeJson,hospitalId)
    s ^GRS("GetBloodInfoV4",barCode_"-")=$lb(barCode,barCodeJson,hospitalId)
    s $zt="errGetBloodInfoV4"   
    s msg="成功",msgcode="999999",status="0" //定义初始
    s bloodbagId="",bloodProductId="",regNO=""
     //判断条码类型--血制品 "BLOODPRODUCT", 血袋 "BLOODBAG" 腕带 "REG"
    s barCodeFlag=..GetBarcodeFlag(barCode) 
    i barCodeJson'="" {
        s barCodeJsonObj=##class(Nur.MNISV3.Common.JSON).Decode(barCodeJson)
        s bloodbagId=barCodeJsonObj.GetAt("bloodbagId")
        s bloodProductId=barCodeJsonObj.GetAt("bloodProductId")
        s regNO=barCodeJsonObj.GetAt("regNo")
    }
    w barCodeFlag,!
    i barCodeFlag="BLOODBAG" s bloodbagId=barCode
    i barCodeFlag="BLOODPRODUCT" s bloodProductId=barCode
    i barCodeFlag="REG" s regNO=barCode
    
    i bloodbagId'=""&&(bloodProductId'="") {
        //获取输血信息
        s bloodInfoObj=..GetBloodInfo(bloodbagId,bloodProductId)
        i bloodInfoObj.GetAt("status")=-1 s msg=bloodInfoObj.GetAt("msg") ,msgcode="999999",status="-1"
        i status="-1" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"108121","-1")
        s bloodInfo = bloodInfoObj.GetAt("bloodInfo")
        s episodeId=bloodInfo.GetAt("episodeId")
        s patRegNo=bloodInfo.GetAt("patRegNo")
        s bloodPref=bloodInfo.GetAt("rowId")
    }
    //判断是否同一个患者
    i ($g(regNO)'="")&&($g(patRegNo)'="")&&(patRegNo'=regNO) q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("非此患者的血袋!!!!请核查！","108121","-1")
    i regNO'="" {
        s episodeId=$P(##class(Nur.MNISV3.Service.Patient).GetPatAdm(regNO),"^",1)
    }
    //i $g(episodeId)'="" s patInfo=##class(Nur.CommonInterface.Patient).getPatient(episodeId)
    i $g(episodeId)'="" s patInfo=##class(Nur.MNISV3.Service.PatientInfo).GetPatient(episodeId)
    //固定提示 msgcode="2022" 
    i (bloodbagId'="")&&(bloodProductId="") s msgcode="2022",msg="请扫描血制品码!^血制品"
    i (bloodbagId="")&&(bloodProductId'="") s msgcode="2022",msg="请扫描血袋码!^血袋"
    i (bloodbagId="")&&(bloodProductId="") s msgcode="2022",msg="请扫描血袋码！或血制品码"
    
    //血袋状态 取血"G",接收"R",输血开始"S",输血巡视"T",输血暂停"SU",输血继续"C",输血结束"E",血袋已回收"RE":
    s bloodStatus="D"
    s recId=$o(^Nur.MNISV3.BloodBagRecI("BarCode"," "_bloodbagId," "_bloodProductId,""),-1)
    i recId'="" {
        s recObj=##class(Nur.MNISV3.BloodBagRec).%OpenId(recId)
        s bloodStatus=recObj.Status
        d recObj.%Close()   
    }
    i bloodStatus="RE" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("输血闭环已完成！","108121","-1")
    w bloodStatus,!
    //模板类型code
    s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hospitalId," BLOOD"," "_bloodStatus,""))
    i nodeId'="" {
        s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
        s showModelCode=nodeObj.ShowModelCode
    }
    //模板配置
    s modelInfo=..GetModelInfoByStatus(bloodStatus,hospitalId)
    
    //节点状态
    s nodeListObj=..GetNodeList(hospitalId,"BLOOD",bloodStatus)
    s nodeStatusList=nodeListObj.GetAt("nodeList")
   
    //按钮（即可操作节点）
    s buttonListObj=..GetButtonList(hospitalId,"BLOOD",bloodStatus)
    s buttonList=buttonListObj.GetAt("nodeList")

    s barCodeJson=##class(%ArrayOfDataTypes).%New()
    d barCodeJson.SetAt($g(bloodbagId),"bloodbagId")
    d barCodeJson.SetAt($g(bloodProductId),"bloodProductId")
    d barCodeJson.SetAt($g(regNO),"regNo")
    d barCodeJson.SetAt($g(bloodPref),"bloodPref")
    d barCodeJson.SetAt($g(episodeId),"episodeId")
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status") 
    //模板类型
    d json.SetAt($g(showModelCode),"showModelCode") 
    d json.SetAt($g(barCodeJson),"barCodeJson")
    //是否刷新 按钮
    i ($g(barCodeFlag)="REG")&&($g(bloodbagId)'="") s refreshBtnFlag="1"
    d json.SetAt($g(refreshBtnFlag),"refreshBtnFlag")
    //判空 解析错误
    i $g(bloodInfo)'="" d json.SetAt($g(bloodInfo),"BLOODINFO") //对应模板FromCode
    i $g(patInfo)'="" d json.SetAt($g(patInfo),"PATINFO")
    d json.SetAt($g(modelInfo),"modelInfo")
    i $g(buttonList)'="" d json.SetAt($g(buttonList),"buttonList")
    i $g(nodeStatusList)'="" d json.SetAt($g(nodeStatusList),"statusList")
    d json.SetAt($g(bloodStatus),"bloodStatus")
    d json.SetAt($g(canBtnFlag),"canBtnFlag")  //按钮是否可操作
    //是否支持，只扫腕带查询输血列表, 默认“”不支持， scanFlag=1支持
    s scanFlag="1"
    d json.SetAt($g(scanFlag),"scanFlag")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetBloodInfoV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"108121","-1")
}

/// 闭环操作
/// w ##class(Nur.MNISV3.Service.Blood).ExecuteBloodV4("0010412334353","P0065V00","123","735","BT","8","20665","","","","","{""5||11"":""jxjxjxjjx"",""5||10"":""hshshh"",""5||2"":""22"",""5||1"":""36.5"",""5||4"":""44"",""5||3"":""33"",""5||6"":""66"",""5||5"":""55"",""5||8"":""有"",""5||7"":""45"",""5||9"":""皮疹^寒战^胸闷""}","0010412334353")
ClassMethod ExecuteBloodV4(bldbagno, bldproductno, bloodPref, episodeId, buttonType, locId, userId, userCode1 = "", pass1 = "", userCode2 = "", pass2 = "", parr = "{}", getBarCode = "")
{
    n (bldbagno,bldproductno,bloodPref,episodeId, buttonType,locId,userId,userCode1,pass1,userCode2,pass2,parr,getBarCode)
    s $zt="errExecuteBloodV4" 
    s ^GRS("ExecuteBloodV4")=$lb(bldbagno,bldproductno,bloodPref,episodeId, buttonType,locId,userId,userCode1,pass1,userCode2,pass2,parr,getBarCode)
    s msg="成功",msgcode="999999",status="0" //定义初始
    i (parr'="") {
        s parrObj=##class(Nur.MNISV3.Common.JSON).Decode(parr)
        s aEvent=parrObj.GetAt("aEvent")
        s ^MML("parr")=$lb(aEvent)
    }
    //用户密码 已验证
    i userCode1'="" s userId1=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userCode1),""))
    i userCode2'="" s userId2=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(userCode2),""))
    
    //限制
    s curDate=+$h
    s curTime=$p($h,",",2)
    s curDateTime=curDate*86400+curTime
    s recId=$o(^Nur.MNISV3.BloodBagRecI("BarCode"," "_bldbagno," "_bldproductno,""),-1)
    i recId'="" {
        s nodeInfoObj=..GetNodeInfo(recId)  
        s getDateTime=nodeInfoObj.GetAt("getDateTime")
        s recDateTime=nodeInfoObj.GetAt("recDateTime")
        s startDateTime=nodeInfoObj.GetAt("startDateTime")
        s endDateTime=nodeInfoObj.GetAt("endDateTime")
        i buttonType="RE"{
            q:((curDateTime-endDateTime)<86400) ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("结束后24小时才能够回收","199999","-1")
        }
    }
    ts
    i $g(userId1)="" s userId1=userId //空 取当前
    //血袋记录--buttonType对应节点Code
    s ret=##class(Nur.MNISV3.BloodBagRec).Save(bldbagno,bldproductno,bloodPref,buttonType, episodeId, userId1, locId)
    i ret["err"  tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(ret,"100003","-1")
    
    //血袋操作--buttonType对应节点Code
    s parrStr="^RecPref|"_ret_"^Status|"_buttonType_"^EpisodeId|"_episodeId_"^RecUser|"_userId1_"^RecDate|"_curDate_"^RecTime|"_curTime //拼接主表ID和状态
    i userCode2'="" s parrStr=parrStr_"^AuditUser|"_userId2_"^AuditDate|"_curDate_"^AuditTime|"_curTime
    s ret=##class(Nur.MNISV3.BloodBagSubRec).Save(parrStr,"", userId1,locId,parr)
    i ret["err"  tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(ret,"100004","-1")
    
    tc
    ///调用接口写在事务外面 start
    i userCode1="" s userCode1=$p($g(^SSU("SSUSR",userId)),"^",1)
    s bloodInfoObj=..GetBloodInfo(bldbagno,bldproductno)
    s bloodInfo = bloodInfoObj.GetAt("bloodInfo")
    s packID=bloodInfo.GetAt("rowId")
    s dateTime=$zd(curDate,3)_" "_$zt(curTime)
    s hosCode=""
    s locCode=""
    s wardCode=""
    i buttonType="R" d ##CLASS(DHCLIS.DHCBloodInterface).UpdatePackArriveInfo(packID, userCode1, dateTime )  //,"", hosCode, locCode, wardCode
    i buttonType="S" d ##CLASS(DHCLIS.DHCBloodInterface).UpdatePackInfusionStartInfo(packID, userCode1, userCode2, dateTime)
    i buttonType="BT" d ##CLASS(DHCLIS.DHCBloodInterface).SavePackScoutInfo(packID,userCode1, 0, dateTime, "", 0)   
    i buttonType="T" d ##CLASS(DHCLIS.DHCBloodInterface).SavePackScoutInfo(packID,userCode1, 1, dateTime, "", 0)
    i buttonType="AT" d ##CLASS(DHCLIS.DHCBloodInterface).SavePackScoutInfo(packID,userCode1, 2, dateTime, "", 0)
    i buttonType="E" d ##CLASS(DHCLIS.DHCBloodInterface).UpdatePackInfusionEndInfo(packID, userCode1,dateTime, "")
    i buttonType="RE" d ##CLASS(DHCLIS.DHCBloodInterface).UpdatePackBackInfo(packID, userCode1, dateTime, 4)
    
    //护理病历
    //s measureDesc=""
    //i $d(%session) s wardId=%session.Get("LOGON.WARDID")
    //d ..SetEmrData(episodeId, curDate, curTime, measureDesc, userId, locId,$g(wardId))
    ///调用接口写在事务外面 end
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errExecuteBloodV4 
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取输血查询和配置
/// w ##class(Nur.MNISV3.Service.Blood).GetBloodDataPreferencesV4("","3","20665","","","")
ClassMethod GetBloodDataPreferencesV4(episodeId, wardId, userId, startDate, endDate, parr)
{
    n (episodeId,wardId,userId,startDate,endDate,parr)
    s ^GRS("GetBloodDataPreferencesV4")=$lb(episodeId,wardId,userId,startDate,endDate,parr)
    s $zt="errGetBloodDataPreferencesV4"   
    s msg="成功",msgcode="999999",status="0" //定义初始
    //顶部tab
    s tabList=##class(%ListOfDataTypes).%New()
    s str1="code=PAT^desc=当前患者^method=GetPatBloodList^num="
    s str2="code=WARD^desc=全病区^method=GetWardBloodList^num="
    i $g(episodeId)'="" {
        s itemData1=..StringToObject(str1)
        d tabList.Insert(itemData1)
    }
    s itemData2=..StringToObject(str2)
    d tabList.Insert(itemData2)
    
    //左侧状态
    s sheetList=##class(%ListOfDataTypes).%New()
    s str0="code=ALL^desc=全部^num="
    s str1="code=START^desc=输血中^num="
    s str2="code=NEED^desc=待输血^Num="
    s str3="code=STOP^desc=暂停中^num="
    s str4="code=END^desc=已完成^num="
    s itemData0=..StringToObject(str0)
    s itemData1=..StringToObject(str1)
    s itemData2=..StringToObject(str2)
    s itemData3=..StringToObject(str3)
    s itemData4=..StringToObject(str4)
    d sheetList.Insert(itemData0)
    d sheetList.Insert(itemData1)
    d sheetList.Insert(itemData2)
    d sheetList.Insert(itemData3)
    d sheetList.Insert(itemData4)
    
    //配置显示字段- 固定字段：status状态，title标题，content内容，content2内容2
    s itemConfig=##class(%ArrayOfDataTypes).%New()
    //bloodList 下一级的bloodStatus 字段，^箭头分割多个
    d itemConfig.SetAt("bloodStatus","status") 
    d itemConfig.SetAt("bloodInfo>productDesc^bloodInfo>patBldGroup","title")
    d itemConfig.SetAt("bloodInfo>bedCode^bloodInfo>patName","content")
    d itemConfig.SetAt("bloodInfo>xmDateTime","content2")
    
    //侧滑按钮
    s slideButtons=##class(%ArrayOfDataTypes).%New()
    d slideButtons.SetAt("slideButtons>desc","desc") ;按钮名称
    d slideButtons.SetAt("recId,userId,slideButtons>code","code") ;取方法参数
    d slideButtons.SetAt("ReturnBloodBag","method") ;调用方法
    d itemConfig.SetAt(slideButtons,"slideButtons")
        
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(tabList),"tabList")
    d json.SetAt($g(sheetList),"sheetList")
    d json.SetAt($g(itemConfig),"itemConfig")
    s stdate=$zd($h-7,3)
    s curdate=$zd($h,3)
    d json.SetAt(stdate,"startDate")
    d json.SetAt(curdate,"endDate")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetBloodDataPreferencesV4   
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取全病区血袋数据
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetBloodDataV4("3","20665","2025-08-11","2025-08-18"))
ClassMethod GetBloodDataV4(wardId, userId, startDate, endDate)
{
    n (wardId,userId, startDate, endDate)
    s ^MML("GetBloodDataV4")=$lb(wardId,userId, startDate, endDate)
    s $zt="errGetBloodDataV4"  
    s msg="成功",msgcode="999999",status="0" //定义初始
    i startDate="" s startDate=+$h
    i endDate="" s endDate=+$h
    s startDate=##class(websys.Conversions).DateHtmlToLogical(startDate)
    s endDate=##class(websys.Conversions).DateHtmlToLogical(endDate)
    s wardBloodList=##class(%ListOfDataTypes).%New()
    // "ctLocDesc": "呼吸内科",  "currLocID": 189, "currWardID": 1,
    //s wardId=1 //测试
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,"ward","")
    s patInfoList=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
        s episodeId=patListObj.GetDataByName("episodeId")
        s patBloodListObj=..GetBloodDataByPatV4(episodeId, startDate, endDate)
        s patBloodList=patBloodListObj.GetAt("bloodList")
        s patStatus=patBloodListObj.GetAt("patStatus")
        s patInfo=##class(Nur.MNISV3.Service.PatientInfo).GetPatient(episodeId)
        s patBloodData=##class(%ArrayOfDataTypes).%New()
        d patBloodData.SetAt(patBloodList,"patBloodList")
        d patBloodData.SetAt(episodeId,"episodeId")
        d patBloodData.SetAt($g(patInfo),"patInfo")
        d patBloodData.SetAt($g(patStatus),"patStatus")
        d wardBloodList.Insert(patBloodData)
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(wardBloodList),"wardBloodList")
    q json
    
errGetBloodDataV4  
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取个人血袋数据
/// w ##class(Nur.MNISV3.Service.Blood).GetBloodDataByPatV4(4113,"2022-07-10","")
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetBloodDataByPatV4("735","2025-08-11","2025-08-18"))
ClassMethod GetBloodDataByPatV4(episodeId, startDate, endDate)
{
    n (episodeId,startDate,endDate)
    s ^MML("GetBloodDataByPatV4")=$lb(episodeId,startDate,endDate)
    s $zt="errGetBloodDataByPatV4"   
    i startDate="" s startDate=+$h
    i endDate="" s endDate=+$h
    s startDate=##class(websys.Conversions).DateHtmlToLogical(startDate)
    s endDate=##class(websys.Conversions).DateHtmlToLogical(endDate)
    s msg="成功",msgcode="999999",status="0" //定义初始
    s bloodList=##class(%ListOfDataTypes).%New()
    f date=startDate:1:endDate
    {
        s recId=""
        f
        {
            s recId=$o(^Nur.MNISV3.BloodBagRecI("DateAdm",date," "_episodeId,recId))
            q:recId=""
            s recObj=##class(Nur.MNISV3.BloodBagRec).%OpenId(recId)
            s bStatus=recObj.Status
            s bloodBagNo=recObj.BloodBagNo
            s bloodProductNo=recObj.BloodProductNo
            d recObj.%Close()
            s bloodInfoObj=..GetBloodInfo(bloodBagNo,bloodProductNo)
            s bloodInfo = bloodInfoObj.GetAt("bloodInfo")
            s bloodData=##class(%ArrayOfDataTypes).%New()
            //后面改成配置或者比较顺序
            s statusStr=..GetStatusByCode(bStatus) //获取输血状态
            s bloodStatus=$p(statusStr,"^",1)
            s patStatus=$p(statusStr,"^",2)
            d bloodData.SetAt(bloodInfo,"bloodInfo")
            d bloodData.SetAt(bloodStatus,"bloodStatus")
            d bloodData.SetAt($g(statusCode),"statusCode")
            d bloodData.SetAt(bloodBagNo,"bloodBagNo")
            d bloodData.SetAt(bloodProductNo,"bloodProductNo")
            d bloodData.SetAt(recId,"recId")
            //侧滑按钮
            s buttons=##class(%ListOfDataTypes).%New()
            s str1="code=BACK^desc=退回^method=ReturnBloodBag"
            d buttons.Insert(..StringToObject(str1))
            i bloodStatus="输血中" {
                s str2="code=DELETE^desc=作废^method=ReturnBloodBag"
                d buttons.Insert(..StringToObject(str2))
            }
            i bloodStatus="已完成" s buttons=##class(%ListOfDataTypes).%New()
            d bloodData.SetAt(buttons,"slideButtons")
            d bloodList.Insert(bloodData)
        }
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(bloodList),"bloodList")
    d json.SetAt($g(patStatus),"patStatus")
    q json
errGetBloodDataByPatV4   
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 通过节点状态码 获取输血状态
/// w ##class(Nur.MNISV3.Service.Blood).GetStatusByCode("R")
ClassMethod GetStatusByCode(bStatus, patStatus = "")
{
    n (bStatus,patStatus)
    s bloodStatus=$s((bStatus="SU"):"暂停中^STOP",bStatus="S"||(bStatus="T")||(bStatus="C"):"输血中^START"
        ,bStatus="E"||(bStatus="AT")||(bStatus="RE"):"已完成^END",1:"待输血^NEED")
    s statusCode=$p(bloodStatus,"^",2)
    s bloodStatus=$p(bloodStatus,"^",1)
    i bloodStatus="输血中" s patStatus=bloodStatus
    i bloodStatus="暂停中"&&(patStatus'="输血中") s patStatus=bloodStatus
    i bloodStatus="待输血"&&((patStatus="")||(patStatus="已完成")) s patStatus=bloodStatus
    i bloodStatus="已完成"&&(patStatus="") s patStatus=bloodStatus
    q bloodStatus_"^"_$g(patStatus)
}

/// 获取输血详情
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetBloodDetailV4("20665","1","8","3","2"))
ClassMethod GetBloodDetailV4(userId, recId, locId, wardId, hospitalId) As %String
{
    n (userId,recId,locId,wardId,hospitalId)
    s $zt="errGetBloodDetailV4"
    s ^GRS("GetBloodDetailV4")=$lb(userId,recId,locId,wardId,hospitalId)
    s msg="成功",msgcode="999999",status="0" //定义初始
    s recObj=##class(Nur.MNISV3.BloodBagRec).%OpenId(recId)
    s bloodStatus=recObj.Status
    s episodeId=recObj.EpisodeId
    s bloodbagId=recObj.BloodBagNo
    s bloodProductId=recObj.BloodProductNo
    d recObj.%Close()
    //节点状态
    s nodeList=##class(%ListOfDataTypes).%New()
    s subId=""
    f
    {
        s subId=$o(^Nur.MNISV3.BloodBagSubRecD(recId,subId))
        q:subId=""
        s nodeData=##class(%ArrayOfDataTypes).%New()
        s subObj=##class(Nur.MNISV3.BloodBagSubRec).%OpenId(recId_"||"_subId)
        s node=subObj.Status
        s date=subObj.RecDate
        s time=subObj.RecTime
        s user=subObj.RecUser
        s user2=subObj.AuditUser
        s recUser=$p(^SSU("SSUSR",user),"^",1) 
        i user2'=""  s recUser=recUser_" / "_$p(^SSU("SSUSR",user2),"^",1) 
        w node,"-",date,!
        s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hospitalId," BLOOD"," "_node,""))
        i nodeId'="" {
            s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
            s patFlag=nodeObj.PatFlag
            s nodeName=nodeObj.NodeName
            s NodeCode=nodeObj.NodeCode
            d nodeObj.%Close() 
        }
        s select=""
        i bloodStatus=node s select="1"
        d nodeData.SetAt(select,"select") //选中
        d nodeData.SetAt($g(recUser),"recUser")
        d nodeData.SetAt($g(node),"nodeCode")
        d nodeData.SetAt($g(nodeName),"nodeName")
        d nodeData.SetAt($zd(date,3),"date")
        d nodeData.SetAt($zt(time),"time")
        d nodeList.Insert(nodeData)  
        i node="G" s getDateTime=date*86400+time
        i node="R" s recDateTime=date*86400+time
        i node="S" s startDateTime=date*86400+time
        i node="E" s endDateTime=date*86400+time
    }
    //获取输血信息
    s bloodInfoObj=..GetBloodInfo(bloodbagId,bloodProductId)
    i bloodInfoObj.GetAt("status")=-1 s msg=bloodInfoObj.GetAt("msg") ,msgcode="999999",status="-1"
    e  s bloodInfo = bloodInfoObj.GetAt("bloodInfo")
    //患者
    i $g(episodeId)'="" s patInfo=##class(Nur.MNISV3.Service.PatientInfo).GetPatient(episodeId)
    s statusStr=..GetStatusByCode(bloodStatus) //获取输血状态
    s bloodStatus=$p(statusStr,"^",1)
    s patStatus=$p(statusStr,"^",2)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(patInfo),"patInfo")
    d json.SetAt($g(nodeList),"nodeList")
    d json.SetAt($g(bloodInfo),"bloodInfo")
    d json.SetAt($g(bloodStatus),"bloodStatus")
    q json
errGetBloodDetailV4
   s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 判断条码类型--血制品 "BLOODPRODUCT", 血袋 "BLOODBAG" 腕带 "REG"
/// w ##class(Nur.MNISV3.Service.Blood).GetBarcodeFlag("P0065V00")
/// w ##class(Nur.MNISV3.Service.Blood).GetBarcodeFlag("0010408040002")
ClassMethod GetBarcodeFlag(barCode = "") As %String
{
    n (barCode)
    s $zt="errGetBarcodeFlag"
    s ^GRS("GetBarcodeFlag")=$lb(barCode)
    s barCodeFlag=""
    s bloodbagId="",bloodProductId="",regNo=""
    //长度10的带字母---先转小写字母 再替换掉
    i ($l(barCode)=10)&&($l($tr($zcvt(barCode,"L"),"abcdefghijklmnopqrstuvwxyz",""))'=$l(barCode)){
         s barCode=$e(barCode,6,10)
    }
    s len=$l(barCode)
    if (barCode["=<")||(barCode["XZP")||(len=5)||(len=7)||(len=8) {
        s barCodeFlag="BLOODPRODUCT" //血制品
        s bloodProductId=barCode
    }elseIf (barCode["=")||(barCode["XD")||(len=12)||(len=13)||(len>10){
        s barCodeFlag="BLOODBAG" //血袋
        s bloodbagId=barCode
    }elseif(len=10){
        s barCodeFlag="REG" //腕带
        s regNo=barCode
    }else{
        q "请确定扫描的条码为血袋码、血制品码或腕带"
    }
    q barCodeFlag
errGetBarcodeFlag
    s Msg=$ze
    q ""
}

/// 通过血袋状态获取模板信息
/// w ##class(Nur.MNISV3.Service.Blood).GetModelInfoByStatus("D","2")
ClassMethod GetModelInfoByStatus(bloodStatus, hospitalId, nodeType = "BLOOD")
{
    n (bloodStatus,hospitalId,nodeType)
    s ^GRS("GetModelInfoByStatus")=$lb(bloodStatus,hospitalId)
    // 输血状态--处于那个节点  例如：^CF.NUR.MNISV3.BloodNodeI("Code"," 2"," "_nodeType," D",1)=""
    s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hospitalId," "_nodeType," "_bloodStatus,""))
    q:nodeId="" ""
    s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
    s showModelCode=nodeObj.ShowModelCode //显示模板Code
    s nodeSub=nodeObj.NodeSub       //可操作节点Code 用!分隔
    s nodeCode=nodeObj.NodeCode
    s SignFlag=nodeObj.SignFlag
    d nodeObj.%Close()
    w showModelCode_"-"_nodeSub,!
    s ^GRS("GetModelInfoByStatus111")=$lb(showModelCode,nodeSub)
    s modelList=##class(%ListOfDataTypes).%New()
    i showModelCode'=""{ //优先取showModelCode
        s modelInfo=..GetModelInfoByModeCode(showModelCode,hospitalId)
        d modelInfo.SetAt($g(nodeCode),"ModelCode")
        d modelInfo.SetAt($g(SignFlag),"SignFlag")  //(loginDouble 双签 loginSingle/空 单签  double 双签  single 单签) LD LS D S
        d modelList.Insert(modelInfo)
    }else{
        f i=1:1:$l(nodeSub,"!") //遍历可操作节点
        {
            s nodeCode=$p(nodeSub,"!",i)
            continue:nodeCode=""
            s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hospitalId," "_nodeType," "_nodeCode,""))   
            continue:nodeId=""
            s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
            s nodeName=nodeObj.NodeName
            s ModelCode=nodeObj.ModelCode
            s SignFlag=nodeObj.SignFlag
            d nodeObj.%Close()
            w "nodeName="_nodeName_"ModelCode="_ModelCode,!
            s modelInfo=..GetModelInfoByModeCode(ModelCode,hospitalId)
            d modelInfo.SetAt($g(nodeCode),"ModelCode")
            d modelInfo.SetAt($g(nodeName),"ModelName")
            d modelInfo.SetAt($g(SignFlag),"SignFlag")
            d modelList.Insert(modelInfo)            
        }
    }
    //w ##class(Nur.MNISV3.Common.JSON).Encode(modelInfo),!
    s ^MML("modellist")= $lb(##class(Nur.MNISV3.Common.JSON).Encode(modelList))
    q modelList
}

/// 通过模板代码获取模板信息
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetModelInfoByModeCode("PATINFO!BLOODINFO","2"))
ClassMethod GetModelInfoByModeCode(modeCode, hospitalId)
{
    n (modeCode,hospitalId)
    s ^GRS("GetModelInfoByModeCode")=$lb(modeCode,hospitalId)
    s formList=##class(%ListOfDataTypes).%New() 
    s subId=""
    f i=1:1:$l(modeCode,"!") //遍历可操作节点
    {
        s formCode=$p(modeCode,"!",i)
        continue:formCode=""
        s formCode=$ZCVT(formCode,"U") //Form组件索引
        s formId=$o(^CF.NUR.MNISV3.ModelFormI("Code"," "_hospitalId," "_formCode,""))
        continue:formId=""
        //w "formId="_formId,!
        s formData=##class(%ArrayOfDataTypes).%New()
        s itemList=##class(%ListOfDataTypes).%New() 
        s itemId=""
        f 
        {
            s itemId=$o(^CF.NUR.MNISV3.ModelItemD(formId,itemId)) //遍历Item配置
            q:itemId=""
            //w "itemId="_formId_"||"_itemId,!
            s itemObj=##class(CF.NUR.MNISV3.ModelItem).%OpenId(formId_"||"_itemId) 
            s position=itemObj.Position
            s itemData=##class(%ArrayOfDataTypes).%New()
            d itemData.SetAt(itemObj.ItemName,"ItemName")
            d itemData.SetAt(itemObj.ItemCode,"ItemCode")
            d itemData.SetAt(itemObj.ItemType,"ItemType")
            d itemData.SetAt(itemObj.ItemNote,"ItemNote")
            d itemData.SetAt(itemObj.ItemSort,"ItemSort")
            //位置 1,2 是第一行 第二列
            d itemData.SetAt(itemObj.Position,"Position")
            //宽度 0.5 屏幕宽度一半
            d itemData.SetAt(itemObj.ItemWidth,"ItemWidth")
            d itemData.SetAt(itemObj.LinkCode,"LinkCode")
            d itemData.SetAt(itemObj.LabelFlag,"LabelFlag")
            d itemData.SetAt(itemObj.SignCode,"SignCode")
            d itemData.SetAt(itemObj.RequiredFlag,"RequiredFlag")
            //对于Android类型: 文本TYPE_CLASS_TEXT=1; 数字TYPE_CLASS_NUMBER=2; TYPE_NUMBER_FLAG_DECIMAL=2000
            d itemData.SetAt(itemObj.KeyboardType,"KeyboardType")
            d itemData.SetAt(itemObj.SimpleFlag,"SimpleFlag")
            d itemData.SetAt(itemObj.DataSource,"DataSource")
            d itemData.SetAt(itemObj.ItemOption,"ItemOption")
            d itemData.SetAt(itemObj.ItemDefault,"ItemDefault")
            //是否能编辑  
            d itemData.SetAt(itemObj.CanEditFlag,"CanEditFlag")
            d itemData.SetAt(formId_"||"_itemId,"itemId")             
            d itemObj.%Close()
            d itemList.Insert(itemData) //插入items
        }
        s fromObj=##class(CF.NUR.MNISV3.ModelForm).%OpenId(formId)
        s FormCode=fromObj.FormCode
        s FormName=fromObj.FormName
        s FormSort=fromObj.FormSort
        s LabelFlag=fromObj.LabelFlag
        s SimpleFlag=fromObj.SimpleFlag 
        d fromObj.%Close()
        d formData.SetAt($g(itemList),"itemList")
        d formData.SetAt($g(FormCode),"FormCode")
        d formData.SetAt($g(FormName),"FormName")
        //显示类型
        s FormType=""
        i ($g(FormCode)="PATINFO")||($g(FormCode)="BLOODINFO") s FormType="1"
        d formData.SetAt($g(FormType),"FormType")
        d formData.SetAt($g(LabelFlag),"LabelFlag")
        d formData.SetAt($g(SimpleFlag),"SimpleFlag")
        d formList.Insert(formData) //插入form
    
    }
    s modelInfo=##class(%ArrayOfDataTypes).%New()
    d modelInfo.SetAt($g(formList),"formList")
    q modelInfo
}

/// 获取按钮列表（即节点）
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetButtonList("2","INFORD","DINFORD"))
ClassMethod GetButtonList(hosId, nodeType, nodeCode)
{
    n (hosId,nodeType,nodeCode)
    s $zt="errGetButtonList"    
    s ^GRS("GetButtonList")=$lb(hosId,nodeType,nodeCode)
    s msg="成功",msgcode="999999",status="0" //定义初始
    s json=##class(%ArrayOfDataTypes).%New()
    s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hosId," "_nodeType," "_nodeCode,""))
    q:nodeId="" json
    s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
    s nodeSub=nodeObj.NodeSub //可操作节点（ 多个用!分隔）
    d nodeObj.%Close()
    s buttonList=##class(%ListOfDataTypes).%New()
    f i=1:1:$l(nodeSub,"!") {
        s nodeCode=$p(nodeSub,"!",i)
        s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hosId," "_nodeType," "_nodeCode,""))
        continue:nodeId=""
        s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
        s nodeName=nodeObj.NodeName
        s patFlag=nodeObj.PatFlag
        s signFlag=nodeObj.SignFlag //校验双签(loginDouble 双签 loginSingle/空 单签  double 双签  single 单签) LD LS D S
        w patFlag,"=",signFlag,"=",nodeCode,!
        s patFlag=$replace(patFlag,$c(0),"")
        d nodeObj.%Close()
        s buttonData=##class(%ArrayOfDataTypes).%New()
        //开始和结束 必须扫腕带
        i (nodeCode="S")||(nodeCode="E") s patFlag="Y"
        d buttonData.SetAt($g(patFlag),"patFlag") 
        d buttonData.SetAt($g(signFlag),"signFlag") 
        d buttonData.SetAt($g(nodeCode),"method") 
        d buttonData.SetAt($g(nodeCode),"code")
        d buttonData.SetAt($g(nodeName),"desc")
        s color=$s(i=1:"#62ABFF",i=2:"#4A95EF",i=3:"#C9E2FF",1:"#4A95EF")
        d buttonData.SetAt(color,"color")
        d buttonList.Insert(buttonData) 
    }
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(buttonList),"nodeList")
    q json
errGetButtonList    
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取闭环节点列表
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetStatusList("2","BLOOD","D"))
ClassMethod GetNodeList(hosId, type, bloodStatus = "")
{
    n (hosId,type,bloodStatus)
    s ^GRS("GetStatusList")=$lb(hosId,type,bloodStatus)
    s $zt="errGetStatusList"    
    s msg="成功",msgcode="999999",status="0" //定义初始
    s nodeList=##class(%ListOfDataTypes).%New()
    k nodeTmp
    s sort=""
    f
    {   //遍历 输血类型节点 sort排序
        s sort=$o(^CF.NUR.MNISV3.BloodNodeI("Type"," "_hosId," "_type,sort))
        q:sort=""
        s nodeId=""
        f
        {
            s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Type"," "_hosId," "_type,sort,nodeId))
            q:nodeId=""
            s sortTmp=$replace(sort," ","")
            s nodeTmp(sortTmp)=nodeId
        }
    }
    s sort=""
    f
    {
        s sort=$o(nodeTmp(sort))
        q:sort=""
        s nodeId=nodeTmp(sort)
        s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
        s validFlag=nodeObj.ValidFlag
        s patFlag=nodeObj.PatFlag
        s nodeName=nodeObj.NodeName
        s NodeCode=nodeObj.NodeCode
        s NodeSort=nodeObj.NodeSort
        d nodeObj.%Close()
        continue:validFlag'="Y"
        s nodeData=##class(%ArrayOfDataTypes).%New()
        s select=""
        s patFlag=$replace(patFlag,$c(0),"")
        i bloodStatus=NodeCode s select="1"
        d nodeData.SetAt(select,"select") //选中
        d nodeData.SetAt(NodeCode,"code")
        d nodeData.SetAt(nodeName,"desc")
        d nodeData.SetAt(patFlag,"patFlag")
        d nodeData.SetAt(NodeSort,"sort")
        d nodeData.SetAt($g(modelInfo),"modelInfo")
        d nodeList.Insert(nodeData) 
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(nodeList),"nodeList")
    q json
errGetStatusList    
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取输血信息---调用LIS接口
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).GetBloodInfo("0010106130005","P0056V00"))
ClassMethod GetBloodInfo(bloodbagId, bloodProductId)
{
    n (bloodbagId,bloodProductId)
    s $zt="errGetBloodInfo" 
    s ^GRS("GetBloodInfo",bloodbagId)=$lb(bloodbagId,bloodProductId)
    s msg="成功",msgcode="999999",status="0" //定义初始
    //LIS接口有变动，报错误--要传UserCode
    s blooInfoStr=##class(DHCLIS.DHCBloodInterface).GetPackInfo(bloodbagId, bloodProductId)
    i blooInfoStr="未找到对应的血液信息" s blooInfoStr=##class(DHCLIS.DHCBloodInterface).GetPackInfo(bloodProductId,bloodbagId)
    i $g(blooInfoStr)["-1^错误" s blooInfoStr=##class(DHCLIS.DHCBloodInterface).GetPackInfo(bloodbagId, bloodProductId,"")
    w blooInfoStr,!
    i ($p(blooInfoStr,"^")="-1")||($g(blooInfoStr)["未查询")||($g(blooInfoStr)["未找到")||($g(blooInfoStr)["不是发血") {
        s status="-1",msg="接口失败: "_blooInfoStr
    }
    d getBloodInfo
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(bloodInfo),"bloodInfo")
    q json
errGetBloodInfo 
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
getBloodInfo
    s bloodInfo=##class(%ArrayOfDataTypes).%New()
    d bloodInfo.SetAt($P(blooInfoStr,"^",1),"rowId")            //血制品rowId
    d bloodInfo.SetAt($P(blooInfoStr,"^",2),"bloodbagId")       //血袋编码  
    d bloodInfo.SetAt($P(blooInfoStr,"^",3),"bloodProductId")   //血制品编码 
    s productDesc=$P(blooInfoStr,"^",4)_" "_$P(blooInfoStr,"^",7)_$P(blooInfoStr,"^",8)
    d bloodInfo.SetAt(productDesc,"productDesc")        //血制品描述
    d bloodInfo.SetAt($P(blooInfoStr,"^",5),"bloodGroupId")     //血型Id
    d bloodInfo.SetAt($P(blooInfoStr,"^",6),"bloodGroup")       //血型    
    d bloodInfo.SetAt($P(blooInfoStr,"^",7),"volume")           //血制品血量 
    d bloodInfo.SetAt($P(blooInfoStr,"^",8),"unit")             //血制品单位
    d bloodInfo.SetAt($P(blooInfoStr,"^",9),"productGroupId")   //血成份Id
    d bloodInfo.SetAt($P(blooInfoStr,"^",10),"productGroup")    //血成份 
    d bloodInfo.SetAt($P(blooInfoStr,"^",11),"storeStatu")      //库存状态
    d bloodInfo.SetAt($P(blooInfoStr,"^",12),"transStatus")     //处理状态  
    d bloodInfo.SetAt($P(blooInfoStr,"^",13),"transDesc")       //处理操作  
    d bloodInfo.SetAt($P(blooInfoStr,"^",14),"expireDateTime")  //失效日期时间    
    d bloodInfo.SetAt($P(blooInfoStr,"^",15),"issueDateTime")    //发血日期时间   
    d bloodInfo.SetAt($P(blooInfoStr,"^",16),"issueUser")           //发血者
    d bloodInfo.SetAt($P(blooInfoStr,"^",17),"xmMajor")         //主侧配血结果
    d bloodInfo.SetAt($P(blooInfoStr,"^",18),"xmMinor")         //次侧配血结果
    d bloodInfo.SetAt($P(blooInfoStr,"^",19),"xmResult")        //交叉配血结果
    d bloodInfo.SetAt($P(blooInfoStr,"^",20),"xmDateTime")          //配血日期时间
    d bloodInfo.SetAt($P(blooInfoStr,"^",21),"xmUser")          //配血者
    d bloodInfo.SetAt($P(blooInfoStr,"^",22),"patName")         //患者姓名  
    d bloodInfo.SetAt($P(blooInfoStr,"^",23),"patSex")          //患者性别  
    d bloodInfo.SetAt($P(blooInfoStr,"^",24),"patAge")          //患者年龄  
    d bloodInfo.SetAt($P(blooInfoStr,"^",25),"patRegNo")           //患者登记号 
    d bloodInfo.SetAt($P(blooInfoStr,"^",26),"patBldGroup")     //患者血型  
    d bloodInfo.SetAt($P(blooInfoStr,"^",27),"location")        //就诊科室  
    d bloodInfo.SetAt($P(blooInfoStr,"^",28),"labRowId")        //检验医嘱rowId 
    d bloodInfo.SetAt($P(blooInfoStr,"^",29),"isTrans")         //是否已经输用
    d bloodInfo.SetAt($P(blooInfoStr,"^",30),"isReaction")      //是否不良反应
    d bloodInfo.SetAt($P(blooInfoStr,"^",31),"isRetrieve")      //是否已回收
    d bloodInfo.SetAt($P(blooInfoStr,"^",32),"episodeId")           //患者就诊号
    ;d bloodInfo.SetAt(4113,"episodeId")        //测试用写死
    d bloodInfo.SetAt($P(blooInfoStr,"^",33),"wardDesc")        //就诊病区  
    d bloodInfo.SetAt($P(blooInfoStr,"^",34),"bedCode")         //床号
}

/// 将String转对象
/// str是字符串，p1:第一层分割符，p2:第二层分隔符
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Blood).StringToObject("code=PAT^desc=当前^num=1")) 
/// 转化对象：{"code":"PAT","desc":"当前","num":1}
ClassMethod StringToObject(str = "", p1 = "^", p2 = "=")
{
    s itemData=##class(%ArrayOfDataTypes).%New()
    f i=1:1:$l(str,p1) {
        s item=$p(str,p1,i)
        q:item="" 
        d itemData.SetAt($p(item,p2,2),$p(item,p2,1))
    }
    q itemData
}

/// Creator:      fengliang
/// CreatDate:    2022-06-27
/// Description:  撤销签收
/// w ##class(Nur.MNISV3.Service.Blood).ReturnBloodBag()
ClassMethod ReturnBloodBag(recId, userId, code = "")
{
    n (recId,userId,code)
    s ^MML("ReturnBloodBag")=$lb(recId,userId,code)
    s $zt="errReturnBloodBag"   
    s msg="成功",msgcode="999999",status="0" //定义初始
    
    i recId'="" {
        s recObj=##class(Nur.MNISV3.BloodBagRec).%OpenId(recId)
        s bloodStatus=recObj.Status
        d recObj.%Close()   
    }
    q:bloodStatus'="R"&&(bloodStatus'="G")&&(bloodStatus'="BT") ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("只有待输血状态的血袋才能退回!",199999,"-1")
    s ret=##class(Nur.MNISV3.BloodBagRec).Delete(recId,userId)
    i ret["err" s msg=ret,msgcode="199999",status="-1"
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errReturnBloodBag   
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取闭环节点列表信息
/// w ##class(Nur.MNISV3.Service.Blood).GetBloodBagNodeList(1)
ClassMethod GetBloodBagNodeList(recId)
{
    n (recId)
    s $zt="errGetNodeList"  
    s ^GRS("GetBloodBagNodeList")=recId
    s msg="成功",msgcode="999999",status="0" //定义初始
    s nodeList=##class(%ListOfDataTypes).%New()
    s hosId=2 //测试
    s subId=""
    f
    {
        s subId=$o(^Nur.MNISV3.BloodBagSubRecD(recId,subId))
        q:subId=""
        s subObj=##class(Nur.MNISV3.BloodBagSubRec).%OpenId(recId_"||"_subId)
        s node=subObj.Status
        s date=subObj.RecDate
        s time=subObj.RecTime
        s recUser=subObj.RecUser
        s auditeUser=subObj.AuditUser
        d subObj.%Close()
        
        i recUser'="" s recUser=$p($g(^SSU("SSUSR",recUser)),"^",2)
        i auditeUser'="" s auditUser=$p($g(^SSU("SSUSR",auditeUser)),"^",2)
        s nodeId=$o(^CF.NUR.MNISV3.BloodNodeI("Code"," "_hosId," "_"BLOOD"," "_node,""))
        continue:nodeId=""
        s nodeObj=##class(CF.NUR.MNISV3.BloodNode).%OpenId(nodeId)
        s nodeName=nodeObj.NodeName
        d nodeObj.%Close()
        s nodeData=##class(%ArrayOfDataTypes).%New()
        d nodeData.SetAt(nodeName,"nodeName")
        d nodeData.SetAt($zd(date,3)_" "_$zt(time,2),"dateTime")
        d nodeData.SetAt($zd(date,3),"date")
        d nodeData.SetAt($zt(time,2),"time")
        d nodeData.SetAt($g(recUser),"recUser")
        d nodeData.SetAt($g(auditUser),"auditUser")
        d nodeList.Insert(nodeData)
    }
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(nodeList),"nodeList")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    q json
errGetNodeList  
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 获取节点信息
/// w ##class(Nur.MNISV3.Service.Blood).GetNodeInfo(10)
ClassMethod GetNodeInfo(recId)
{
    n (recId)
    s $zt="errGetNodeInfo"  
    s msg="成功",msgcode="999999",status="0" //定义初始
    s subId=""
    f
    {
        s subId=$o(^Nur.MNISV4.BloodBagSubRecD(recId,subId))
        q:subId=""
        s subObj=##class(Nur.MNISV4.BloodBagSubRec).%OpenId(recId_"||"_subId)
        s node=subObj.Status
        s date=subObj.RecDate
        s time=subObj.RecTime
        i node="G" s getDateTime=date*86400+time
        i node="R" s recDateTime=date*86400+time
        i node="S" s startDateTime=date*86400+time
        i node="E" s endDateTime=date*86400+time
    }
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(getDateTime),"getDateTime")
    d json.SetAt($g(recDateTime),"recDateTime")
    d json.SetAt($g(startDateTime),"startDateTime")
    d json.SetAt($g(endDateTime),"endDateTime")
    q json
errGetNodeInfo  
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV4.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// 同步护理病历
ClassMethod SetEmrData(episodeId, date, time, measureDesc, userId, locId, wardId)
{
    n (episodeId, date, time, measureDesc, userId, locId, wardId)
    s userCode=$p($g(^SSU("SSUSR",userId)),"^",1)
    s userName=$p($g(^SSU("SSUSR",userId)),"^",2)
    s emrGuId="c78e92901f934878a3ca598ec364d193"
    s parrObj=##class(%ArrayOfDataTypes).%New()
    d parrObj.SetAt($zd(date,3),"DateElement_2")
    d parrObj.SetAt($zt(time,2),"TimeElement_4")
    d parrObj.SetAt(measureDesc,"TextareaElement_42")
    d parrObj.SetAt("CA"_userName_"*"_userCode,"TextElement_44")
    d parrObj.SetAt(1,"TemporarySave")
    d parrObj.SetAt("2022-07-14(16:54)180 1-测试","HiddenTextElement_51")
    d parrObj.SetAt(emrGuId,"TemporarySave")
    s SessionVal=##class(%ArrayOfDataTypes).%New()
    i ($d(%session)) d
    .d SessionVal.SetAt(%session.Get("LOGON.CTLOCID"),"LOGON.CTLOCID")
    .d SessionVal.SetAt(%session.Get("LOGON.WARDID"),"LOGON.WARDID")
    .d SessionVal.SetAt(%session.Get("LOGON.GROUPDESC"),"LOGON.GROUPDESC")
    .d SessionVal.SetAt(%session.Get("LOGON.USERID"),"LOGON.USERID")
    s SessionVal="^"_##class(Nur.MNISV3.Common.JSON).Encode(SessionVal)
    s ret=##class(NurMp.Template.PDASaveInterface).save(emrGuId, episodeId, "", parrObj, "住院护士", locId, wardId, userId, 1,SessionVal)
    q ret
}

}
